		
		package com.cx.bank.Dao;

		import java.io.*;
		import java.util.*;
		import com.cx.bank.model.MoneyBean;
		import com.cx.bank.model.UserBean;
		import com.cx.bank.util.MD5Util;
		import com.cx.bank.util.MyIoException;
		
		/**
		 * 实现银行持久层接口的各个操作的类
		 * @author dengqin
		 * @version 2017/6/25
		 */
		
		public class Impllog_res implements  Log_Res{
			//static Properties props;
		
			private static Impllog_res instance;//建立单例对象
			 
			 /**
			  * 将构造方法设为私有建立单例模型
			  */
			 
			private Impllog_res(){
				
			}
			
			/**
			 * 建立单例模型
			 * @return instance
			 */
			public static Impllog_res getInstance(){
				if(instance == null){
					instance = new Impllog_res();
				}
				return instance;
			}
			

			/**
			 * 实现注册操作
			 * @param name
			 * @param pass
			 * @return flag
			 */
			
			@Override
			public boolean register(UserBean user,MoneyBean money) throws IOException {
				// TODO Auto-generated method stub
				boolean flag = false;
				MD5Util md =new MD5Util();
				Properties 	props =new 	Properties();
				File f =new File(".\\"+ user.getUsername() +".properties");
				if(f.exists()){
					flag= false;
					throw new MyIoException("这个用户名已经注册过了!请重新注册！");
				}
				flag =true;
				FileInputStream in = new FileInputStream("src/Bank.properties");
				props.load(in);
				in.close();
				
				 FileOutputStream out =new FileOutputStream(".\\"+ user.getUsername() +".properties");
				 props.setProperty("username", user.getUsername() );
				 props.setProperty("password", user.getPassword());
				 props.setProperty("money", 10.0+"");
				 props.setProperty("id_card","622"+money.getId());//设置卡号
				
			//注册成功后对username、password进行加密
						   Enumeration<?> names=props.propertyNames();

				            while(names.hasMoreElements()){

				               String name=(String)names.nextElement();
				               
				             //只对username和password进行加密
				               if("money".equals(name)||"id_card".equals(name)) continue;
				               
				               String value=props.getProperty(name);
								 String s = md.getMD5(value);//
								 props.setProperty(name,s); 
								
				            }
				            props.store(out, "comments");
							 out.close();
				 return flag;
			}
		
			/**
			 * 实现用户登录的操作
			 * @param name
			 * @param pass
			 * @return flag
			 */
			
			@Override
			public boolean login(UserBean user) throws IOException {
				MD5Util md =new MD5Util();
				MoneyBean money = MoneyBean.getInstance();//定义money对象
				boolean flag = false;
				Properties 	props =new 	Properties();
				File f =new File(".\\"+ user.getUsername() +".properties");
				if(!(f.exists())){
					flag= false;
					throw new MyIoException("这个用户还没有注册! 请先注册！");
				}
				FileInputStream in = new FileInputStream(f);
				props.load(in);
			
				String name = md.getMD5(user.getUsername());//对输入的用户名加密
			//	System.out.println("用户名为："+name);
				String pass = md.getMD5(user.getPassword());//输入的用户密码加密
				//System.out.println("用户密码为："+pass);
			//	System.out.println((props.getProperty("username").equals(name))&& (props.getProperty("password").equals(pass)));
			
			if((props.getProperty("username").equals(name))&& (props.getProperty("password").equals(pass))){
					flag = true;
				}else{
					flag = false;
					throw new  MyIoException("用户名或密码错误! 请重新输入！");
				}
			money.setMoney(Double.parseDouble(props.getProperty("money")));//把默认的余额存入该用户名下
	
				return flag;
			}
			
		/**
		 * 用户转账
		 * @param money
		 * @param name
		 * @return flag
		 */
			
			@Override
			public boolean transfer(double money, String name)throws Exception {
				// TODO Auto-generated method stub
				Properties  props = new 	Properties();
				MD5Util md =new MD5Util();//定义加密对象
				MoneyBean mon =MoneyBean.getInstance();
				boolean flag = false;
		//	System.out.println(name);
				File f =new File(".\\"+name+".properties");
				if(!(f.exists())){
					flag = false;
					throw new MyIoException("这个用户不存在! 请重新输入！");
				}
				FileInputStream in = new FileInputStream(f);
				props.load(in);
				//System.out.println(props.getProperty("money"));
				double mone = Double.parseDouble(props.getProperty("money"));//先得到一开始有的money
				String u_name =props.getProperty("username");
				String id =props.getProperty("id_card");
				//System.out.println(u_name);
				in.close();
				String uname = md.getMD5(name);//对输入的用户名加密
				//System.out.println(uname);
				//System.out.println(id);
				//System.out.println(u_name.equals(uname));
				//System.out.println(id.equals(mon.getId()+""));
				if(u_name.equals(uname)&& id.equals(mon.getId()+"")){
					flag =true;
					double r_money = mone+money;
					System.out.println("转账后的余额为："+r_money);
				
					FileOutputStream out =new FileOutputStream(".\\"+name+".properties");
					props.setProperty("money", r_money+"");
					props.store(out," comments");
					out.close();
				}
				return flag;
			}
			
			
			/**
			 * 退出系统之前把存款/取款后的余额存入
			 * @return nothing;
			 */
			
			public void addMoney(double money,UserBean user) throws IOException {
				// TODO Auto-generated method stub
				//MoneyBeanmoney=MoneyBean.getInstance();
				Properties props =new Properties();
				MD5Util md =new MD5Util();
					FileInputStream in = new FileInputStream("src/Bank.properties");
					props.load(in);
					in.close();

					FileOutputStream  out = new FileOutputStream(".\\"+user.getUsername()+".properties");
				//	System.out.println(props.getProperty("money"));
					String name = md.getMD5(user.getUsername());//对输入的用户名加密
					String pass = md.getMD5(user.getPassword());//输入的用户密码加密
					props.setProperty("username",name);
					props.setProperty("password", pass);
				//	System.out.println(money);
				//	System.out.println(props.propertyNames());
					props.setProperty("money", money+"");//把余额存入money中
					props.store(out, "comments");
					out.close();
			}
		}
